package com.youhome.service.service.impl;



import com.youhome.service.mapper.HJInterestPointMapper;
import com.youhome.service.model.HJInterestPoint;
import com.youhome.service.model.SquareCoordinate;
import com.youhome.service.service.HJInterestPointService;
import com.youhome.utils.GeoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chen365 on 2018/07/09.
 */
@Service
public class HJInterestPointServiceImpl implements HJInterestPointService {
    @Autowired
    private HJInterestPointMapper hjInterestPointMapper;

    @Override
    public List<HJInterestPoint> selectByBean(double lat, double lng, double distance) {
        HJInterestPoint record = new HJInterestPoint();
        record.setLatitude(lat);
        record.setLongitude(lng);

        SquareCoordinate squareCoordinate = getSquareCoordinate(lat, lng, distance);
        record.setMinLat(squareCoordinate.getMinLat());
        record.setMaxLat(squareCoordinate.getMaxLat());
        record.setMinLng(squareCoordinate.getMinLng());
        record.setMaxLng(squareCoordinate.getMaxLng());
        record.setDistance(distance);

        return hjInterestPointMapper.selectByBean(record);
    }


    /**
     * 通过点坐与距离标获正方形坐标
     *
     * @param lat
     * @param lng
     * @param distance
     * @return
     */
    public static SquareCoordinate getSquareCoordinate(double lat, double lng, double distance) {
        SquareCoordinate squareCoordinate = new SquareCoordinate();
        double r = 6378.137;//地球半径千米
        double dLng = 2 * Math.asin(Math.sin(distance / (2 * r)) / Math.cos(lat * Math.PI / 180));
        dLng = dLng * 180 / Math.PI;//角度转为弧度
        double dLat = distance / 1000 / r;
        dLat = dLat * 180 / Math.PI;
        double minLat = lat - dLat;
        double maxLat = lat + dLat;
        double minLng = lng - dLng;
        double maxLng = lng + dLng;

        squareCoordinate.setMinLat(minLat);
        squareCoordinate.setMaxLat(maxLat);
        squareCoordinate.setMinLng(minLng);
        squareCoordinate.setMaxLng(maxLng);

        return squareCoordinate;
    }
}
